<?php




  function getToken() {

    if(!isset($_SESSION['user']['fid']) || empty($_SESSION['user']['fid'])){
      echo 'ko lay dc token - user ko hop le';
      die();
    }

    global $restclient, $server_uri;
    
    $data = array ();
    $data ['pid'] = PARTNER_ID; // parterid
    $data ['ppass'] = PARTNER_PASSWORD; // partner password
    $data ['action'] = 'getaccesstoken'; // action      

    if (! is_null ( $data )) {
      $restclient->postWebRequest ( $server_uri, $data );
      $result = $restclient->getWebResponse ();
    } else {
      $restclient->getWebRequest ( $server_uri );
      $result = $restclient->getWebResponse ();
    }                                  

    if (is_array ( json_decode ( $result, true ) )) {
      $result = json_decode ( $result );
      if($result->code == 1){
        return $result;  
      }  
    } 
    print_r ( $result );
    echo "\n<br />";
    die ( 'ERROR: method `' . __FUNCTION__  . '`' );
  }



  /**
  * create sign ssl rsa for request
  * 
  * @author tientm
  * @param mixed $data
  * @return string
  */
  function createSign(&$data, &$sign) {

    // fetch private key from file and ready it                             
    $fp = fopen ( dirname ( __FILE__ ) . "/server.key", "r" );
    $private_key = fread ( $fp, 8192 );
    fclose ( $fp );

    // At least with PHP 5.2.2 / OpenSSL 0.9.8b (Fedora 7)
    // there seems to be no need to call openssl_get_privatekey or similar.
    // Just pass the key as defined above 
    openssl_sign ( $data, $sign, $private_key, OPENSSL_ALGO_SHA1 );

    $sign = base64_encode ( $sign );
    return $sign;
  }

  /**
  * check sign ssl rsa                   
  * 
  * @author tientm
  * @param mixed $key2
  * @param mixed $data
  * @return int boolean
  */
  function CheckSign(&$data, &$sign) {

    // fetch public key from file and ready it                             
    $fp = fopen ( dirname ( __FILE__ ) . "/server.crt", "r" );
    $public_key = fread ( $fp, 8192 );
    fclose ( $fp );

    // check sign rsa ssl and return boolean
    return openssl_verify ( $data, base64_decode ( $sign ), $public_key, OPENSSL_ALGO_SHA1 );
  }    

?>
